home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / ShapesDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.4 KB  |  135 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This displays a framed area containing one of each shape you
  22.  * can draw.
  23.  */
  24.  
  25. public class ShapesDemo extends Applet {
  26.     final static int maxCharHeight = 15;
  27.  
  28.     public void init() {
  29.         validate();
  30.     }
  31.  
  32.     public void paint(Graphics g) {
  33.         Dimension d = size();
  34.         int x = 5;
  35.         int y = 7;
  36.  
  37.         Color bg = getBackground();
  38.         Color fg = getForeground();
  39.  
  40.         int gridWidth = d.width / 7;
  41.         int gridHeight = d.height / 2;
  42.         int stringY = gridHeight - 7;
  43.         int rectWidth = gridWidth - 2*x;
  44.         int rectHeight = stringY - maxCharHeight - y;
  45.  
  46.         g.setColor(bg);
  47.         g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
  48.         g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
  49.         g.setColor(fg);
  50.  
  51.         // drawLine() 
  52.         g.drawLine(x, y+rectHeight-1, x + rectWidth, y); // x1, y1, x2, y2
  53.         g.drawString("drawLine()", x, stringY);
  54.         x += gridWidth;
  55.  
  56.         // drawRect() 
  57.         g.drawRect(x, y, rectWidth, rectHeight); // x, y, width, height
  58.         g.drawString("drawRect()", x, stringY);
  59.         x += gridWidth;
  60.  
  61.         // draw3DRect() 
  62.         g.setColor(bg);
  63.         g.draw3DRect(x, y, rectWidth, rectHeight, true);
  64.         g.setColor(fg);
  65.         g.drawString("draw3DRect()", x, stringY);
  66.         x += gridWidth;
  67.  
  68.         // drawRoundRect() 
  69.         g.drawRoundRect(x, y, rectWidth, rectHeight, 10, 10); // x, y, w, h, arcw, arch
  70.         g.drawString("drawRoundRect()", x, stringY);
  71.         x += gridWidth;
  72.  
  73.         // drawOval() 
  74.         g.drawOval(x, y, rectWidth, rectHeight); // x, y, w, h
  75.         g.drawString("drawOval()", x, stringY);
  76.         x += gridWidth;
  77.  
  78.         // drawArc() 
  79.         g.drawArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
  80.         g.drawString("drawArc()", x, stringY);
  81.         x += gridWidth;
  82.  
  83.         // drawPolygon() 
  84.         Polygon polygon = new Polygon();
  85.         polygon.addPoint(x, y);
  86.         polygon.addPoint(x+rectWidth, y+rectHeight);
  87.         polygon.addPoint(x, y+rectHeight);
  88.         polygon.addPoint(x+rectWidth, y);
  89.         //polygon.addPoint(x, y); //don't complete; fill will, draw won't
  90.         g.drawPolygon(polygon); 
  91.         g.drawString("drawPolygon()", x, stringY);
  92.  
  93.         x = 5 + gridWidth;
  94.         y += gridHeight;
  95.         stringY += gridHeight;
  96.  
  97.         // fillRect() 
  98.         g.fillRect(x, y, rectWidth, rectHeight); // x, y, width, height
  99.         g.drawString("fillRect()", x, stringY);
  100.         x += gridWidth;
  101.  
  102.         // fill3DRect() 
  103.         g.setColor(bg);
  104.         g.fill3DRect(x, y, rectWidth, rectHeight, true);
  105.         g.setColor(fg);
  106.         g.drawString("fill3DRect()", x, stringY);
  107.         x += gridWidth;
  108.  
  109.         // fillRoundRect() 
  110.         g.fillRoundRect(x, y, rectWidth, rectHeight, 10, 10); // x, y, w, h, arcw, arch
  111.         g.drawString("fillRoundRect()", x, stringY);
  112.         x += gridWidth;
  113.  
  114.         // fillOval() 
  115.         g.fillOval(x, y, rectWidth, rectHeight); // x, y, w, h
  116.         g.drawString("fillOval()", x, stringY);
  117.         x += gridWidth;
  118.  
  119.         // fillArc() 
  120.         g.fillArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
  121.         g.drawString("fillArc()", x, stringY);
  122.         x += gridWidth;
  123.  
  124.         // fillPolygon() 
  125.         Polygon filledPolygon = new Polygon();
  126.         filledPolygon.addPoint(x, y);
  127.         filledPolygon.addPoint(x+rectWidth, y+rectHeight);
  128.         filledPolygon.addPoint(x, y+rectHeight);
  129.         filledPolygon.addPoint(x+rectWidth, y);
  130.         //filledPolygon.addPoint(x, y);
  131.         g.fillPolygon(filledPolygon); 
  132.         g.drawString("fillPolygon()", x, stringY);
  133.     }
  134. }
  135.